import os
import settings
   
def current_dir(file):
    """
    return the current projectdir
    """
    if 'linux' in os.sys.platform:
        dir_array = os.path.dirname(file).split('/')
        current = "/".join(dir_array[:len(dir_array)-1])
    if 'win32' in os.sys.platform:
        dir_array = os.path.dirname(file).split('\\')
        current = "\\".join(dir_array[:len(dir_array)-1])
    return current

def mkdirs(newdir):
    """
        works the way a good mkdir should :)
        - already exists, silently complete
        - regular file in the way, raise an exception
        - parent directory(ies) does not exist, make them as well
    """
    if os.path.isdir(newdir):
        pass
    elif os.path.isfile(newdir):
        raise OSError("a file with the same name as the desired " \
                      "dir, '%s', already exists." % newdir)
    else:
        head, tail = os.path.split(newdir)
        if head and not os.path.isdir(head):
            mkdirs(head)
        if tail:
            os.mkdir(newdir)

def post_message(request, message):
    if request.user.is_authenticated():
        request.user.message_set.create(message=message)
    else:
        request.session.setdefault('anonymous_user_messages', [])
        request.session['anonymous_user_messages'].append(message)